package com.alcatel_lucent.osp.service;

import com.alcatel_lucent.osp.model.Attachment;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * User: guoshunw
 * Date: 13-8-28
 * Time: 下午2:39
 */

@Service
public class AttachmentServiceImpl extends BaseServiceImpl implements AttachmentService {


    @Override
    public Attachment findAttachmentById(Long Id) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", Id);
        return (Attachment) dao.retrieve(Attachment.class, Id);
    }

    @Override
    public InputStream getAttachmentInputStream(Long Id) throws SQLException, IOException {
        Attachment attachment = findAttachmentById(Id);

        return new ByteArrayInputStream(attachment.getContent());
//        File tmpFile = new File(FileUtils.getTempDirectory(), attachment.getName());
//        FileUtils.copyInputStreamToFile(blob.getBinaryStream(), tmpFile);
//        return FileUtils.openInputStream(tmpFile);
    }

    @Override
    public Attachment createAttachment(Attachment attachment) {
       return (Attachment) dao.create(attachment);
    }
}
